<!DOCTYPE html>
<html lang="en">
<head>
<link href="usuario.css" rel="stylesheet" type="text/css">
</head>
<body>
<p> Control de usuarios</p>
<?php require_once('../dbtest/Connections/cn.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
  $insertSQL = sprintf("INSERT INTO usuario (usuario, contrasenia) VALUES (%s, %s)",
                       GetSQLValueString($_POST['usuario'], "text"),
                       GetSQLValueString($_POST['contrasenia'], "text"));

  mysql_select_db($database_cn, $cn);
  $Result1 = mysql_query($insertSQL, $cn) or die(mysql_error());

  $insertGoTo = "../tabla/dbtest/usuario.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}

$maxRows_rsUsuario = 10;
$pageNum_rsUsuario = 0;
if (isset($_GET['pageNum_rsUsuario'])) {
  $pageNum_rsUsuario = $_GET['pageNum_rsUsuario'];
}
$startRow_rsUsuario = $pageNum_rsUsuario * $maxRows_rsUsuario;

mysql_select_db($database_cn, $cn);
$query_rsUsuario = "SELECT * FROM usuario";
$query_limit_rsUsuario = sprintf("%s LIMIT %d, %d", $query_rsUsuario, $startRow_rsUsuario, $maxRows_rsUsuario);
$rsUsuario = mysql_query($query_limit_rsUsuario, $cn) or die(mysql_error());
$row_rsUsuario = mysql_fetch_assoc($rsUsuario);

if (isset($_GET['totalRows_rsUsuario'])) {
  $totalRows_rsUsuario = $_GET['totalRows_rsUsuario'];
} else {
  $all_rsUsuario = mysql_query($query_rsUsuario);
  $totalRows_rsUsuario = mysql_num_rows($all_rsUsuario);
}
$totalPages_rsUsuario = ceil($totalRows_rsUsuario/$maxRows_rsUsuario)-1;
?>
</p>
<p>&nbsp; </p>
<table width="200" border="2" align="center" class="slider">
  <tr>
    <td bgcolor="#FFCC99"><em>Id</em></td>
    <td bgcolor="#FFCC99"><em>Usuario</em></td>
    <td bgcolor="#FFCC99"><em>Contraseña</em></td>
    <td bgcolor="#FFCC99"><em>Acciones</em></td>
  </tr>
  <?php do { ?>
  <tr>
    <td><em><?php echo $row_rsUsuario['id_usuario']; ?></em></td>
    <td><em><?php echo $row_rsUsuario['usuario']; ?></em></td>
    <td><em><?php echo $row_rsUsuario['contrasenia']; ?></em></td>
    <td><em><a href="editar_us.php?recordID=<?php echo $row_rsUsuario['id_usuario']; ?>"</em>
      
      <p><em><img src="../tabla/dbtest/images/editar.png"></em></p>
      <p><em><a href="../tabla/dbtest/borrar_us.php?recordID=<?php echo $row_rsUsuario['id_usuario']; ?>"><img src="../tabla/dbtest/images/borrar.png"></a></em></p></td>
  </tr>
  <?php } while ($row_rsUsuario = mysql_fetch_assoc($rsUsuario)); ?>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>
<?php
mysql_free_result($rsUsuario);
?>
<h1> Crear nuevo usuario</h1>
<form method="post" name="form1" action="<?php echo $editFormAction; ?>">
  <table align="center">
    <tr valign="baseline">
      <td nowrap align="right">Usuario:</td>
      <td><input type="text" name="usuario" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Contrasenia:</td>
      <td><input type="password" name="contrasenia" value="" size="32"></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">&nbsp;</td>
      <td><input type="submit" value="Insertar registro"></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1">
</form>
<p>&nbsp;</p>
</body>
</html>
